package com.sgb.kjwl.viewmodel.self.publish;

import android.text.TextUtils;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.LeaseStateEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.self.publish.SelfLeaseListActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SelfLeaseListModel extends BaseViewModel {
    public ObservableField<Map> map = new ObservableField<>(new HashMap());
    private final SelfLeaseListActivity activity;
    private int page = 1;
    private final int size = 10;

    public SelfLeaseListModel(SelfLeaseListActivity activity) {
        this.activity = activity;
    }

    //获取租赁列表
    public void getPublishListModel(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        map.get().put("current", page);
        map.get().put("limit", size);
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().getLeasePersonalList(map.get())
                .enqueue(new Callback<BaseEntity<ListResponse<List<LeaseStateEntity>>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<ListResponse<List<LeaseStateEntity>>>> call, Response<BaseEntity<ListResponse<List<LeaseStateEntity>>>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            activity.showList(response.body().getData().getList(), isLoadMore);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<ListResponse<List<LeaseStateEntity>>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    //上下架机械
    public void upDownMachineStateModel(String status, String id) {
        APIManager.getInstance().getLeaseAPI().offShelfMachine(id, status)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            if ("1".equals(status)) {
                                MToast.showToast(activity, "上架成功");
                            } else {
                                MToast.showToast(activity, "下架成功");
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    //删除机械
    public void delMachineModel(String id) {
        APIManager.getInstance().getLeaseAPI().deleteMachine(id)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            MToast.showToast(activity, "删除成功");
                            getPublishListModel(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                    }
                });
    }

    //上下架设备
    public void upDownDeviceStateModel(String opt, String id) {
        APIManager.getInstance().getLeaseAPI().offShelfDevice(id,opt)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            if ("1".equals(opt)) {
                                MToast.showToast(activity, "上架成功");
                            } else {
                                MToast.showToast(activity, "下架成功");
                            }
                            getPublishListModel(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                    }
                });
    }

    //删除设备
    public void delDeviceModel(String id) {
        APIManager.getInstance().getLeaseAPI().deleteDevice(id)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            MToast.showToast(activity, "删除成功");
                            getPublishListModel(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                    }
                });
    }

    //上下架车辆
    public void upDownCarStateModel(String opt, String id) {
        APIManager.getInstance().getUserAPI().upDownCarState(id, opt)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            if ("1".equals(opt)) {
                                MToast.showToast(activity, "上架成功");
                            } else {
                                MToast.showToast(activity, "下架成功");
                            }
                            getPublishListModel(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                    }
                });
    }

    //删除车辆
    public void delCarModel(String id) {
        Map map = new HashMap();
        List<String> ids = new ArrayList<>();
        ids.add(id);
        map.put("ids", ids);
        APIManager.getInstance().getUserAPI().upDownCarState(map)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            MToast.showToast(activity, "删除成功");
                            getPublishListModel(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {

                    }
                });
    }

    //上下架房产
    public void upDownHouseStateModel(String opt, String id) {
//        Map map = new HashMap();
//        map.put("status", opt);
//        map.put("id", id);
        APIManager.getInstance().getLeaseAPI().offShelfHouse(id,opt)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            if ("1".equals(opt)) {
                                MToast.showToast(activity, "上架成功");
                            } else {
                                MToast.showToast(activity, "下架成功");
                            }
                            getPublishListModel(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                    }
                });
    }

    //删除房产
    public void delHouseModel(String id) {
        APIManager.getInstance().getLeaseAPI().deleteHouse(id)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            MToast.showToast(activity, "删除成功");
                            getPublishListModel(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                    }
                });
    }

    //土地上下架
    public void upDownLandModel(String opt, String id) {
        DialogHelper.showProgressDialog(activity, null, "加载中...", 0, false, null);
        APIManager.getInstance().getLeaseAPI().offShelfLand(id, opt)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            if ("1".equals(opt)) {
                                MToast.showToast(activity, "上架成功");
                            } else {
                                MToast.showToast(activity, "下架成功");
                            }
                            getPublishListModel(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    //土地删除
    public void delLandModel(String id) {
        DialogHelper.showProgressDialog(activity, null, "加载中...", 0, false, null);
        APIManager.getInstance().getLeaseAPI().deleteLand(id)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            MToast.showToast(activity, "删除成功");
                            getPublishListModel(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    //上下架
    public void upDownFactoryStateModel(String opt, String id) {
        DialogHelper.showProgressDialog(activity, null, "加载中...", 0, false, null);
        APIManager.getInstance().getLeaseAPI().offShelfFactory(id, opt)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            if ("1".equals(opt)) {
                                MToast.showToast(activity, "上架成功");
                            } else {
                                MToast.showToast(activity, "下架成功");
                            }
                            getPublishListModel(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    //删除
    public void delFactoryModel(String id) {
        DialogHelper.showProgressDialog(activity, null, "加载中...", 0, false, null);
        APIManager.getInstance().getLeaseAPI().deleteFactory(id)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            MToast.showToast(activity, "删除成功");
                            getPublishListModel(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    //农机上下架
    public void upDownFarmModel(String opt, String id) {
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().offShelfFarm(id, opt)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            if ("1".equals(opt)) {
                                MToast.showToast(activity, "上架成功");
                            } else {
                                MToast.showToast(activity, "下架成功");
                            }
                            getPublishListModel(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    //农机删除
    public void delFarmModel(String id) {
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().deleteFarm(id)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            MToast.showToast(activity, "删除成功");
                            getPublishListModel(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    public void getH5Url(String type, String id) {
        String name = "";
        if ("1".equals(type)) {
            name = "mechanicsDetail";//机械
        } else if ("2".equals(type)) {
            name = "equipmentDetails";//设备
        } else if ("3".equals(type)) {
            name = "vehicleDetails";//车辆
        } else if ("4".equals(type)) {
            name = "propertyDetails";//房产
        } else if ("5".equals(type)) {
            name = "landDetails";//土地
        } else if ("6".equals(type)) {
            name = "factoryDetails";//工厂
        } else if ("7".equals(type)) {
            name = "farmDetail";//农机
        }
        if (TextUtils.isEmpty(name)) {
            return;
        }
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(name).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id + "&iosFlag=2";
                    WebViewX5HostActivity.start(activity, url);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
